package com.gem.controller;

import com.gem.factory.user.UserFactory;
import com.gem.service.UserService;
import com.gem.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Author: Xhy
 * CreateTime: 2022-09-21 14:21
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 创建用户
     * @param name
     * @return
     */
    @PostMapping("/create")
    public R create(String name){
        return userService.createUser(name);
    }


    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(String userId){
        return userService.remove(userId);
    }


    /**
     * 用户列表
     * @return
     */
    @GetMapping("/list")
    public R list(){
        return userService.list();
    }

    /**
     * 查询用户是否存在
     * @param userId
     * @return
     */
    @GetMapping("/exist")
    public R exist(String userId){
        if (!UserFactory.existUser(userId)){
            return R.error();
        }
        return R.ok();
    }

}
